'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.genNs = exports.removeById = exports.getById = exports.get = exports.add = exports.genId = undefined;

var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');

var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);

var _startsWith = require('lodash/startsWith');

var _startsWith2 = _interopRequireDefault(_startsWith);

var _filter = require('lodash/filter');

var _filter2 = _interopRequireDefault(_filter);

var _removeArrayItem = require('./removeArrayItem');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var manager = {};
var id = 0;
var genId = exports.genId = function genId() {
    return 'genNs_' + id++;
};
var _add = function _add(data, ns) {
    for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
        args[_key - 2] = arguments[_key];
    }

    var id = genId();
    var item = {
        id: id,
        path: ns,
        value: args
    };
    data.push(item);
    data._map[id] = item;
    return item;
};
exports.add = _add;
var genNsFilter = function genNsFilter(ns, fn) {
    return function (item) {
        var path = item.path,
            _item$value = item.value,
            value = _item$value === undefined ? [] : _item$value;

        var flag = false;
        if (typeof ns === 'string') {
            flag = (0, _startsWith2.default)(path, ns);
        } else if (ns instanceof RegExp) {
            flag = ns.test(path);
        }
        if (!flag || !fn) {
            return flag;
        }
        return fn === value[0];
    };
};
var _get = function _get(data, ns, fn) {
    if (!ns) {
        return [].concat((0, _toConsumableArray3.default)(data));
    }
    var nsFilter = genNsFilter(ns, fn);
    return (0, _filter2.default)(data, nsFilter);
};
exports.get = _get;
var _getById = function _getById(data, id) {
    return data._map[id];
};
exports.getById = _getById;
var _removeById = function _removeById(data, id) {
    var item = _getById(data, id);
    if (!item) {
        return;
    }
    var index = data.indexOf(item);
    data.splice(index, 1);
    delete data._map[id];
};
exports.removeById = _removeById;
var genNs = exports.genNs = function genNs() {
    var data = [];
    data._map = {};
    return {
        getData: function getData() {
            return data;
        },

        add: function add() {
            for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
                args[_key2] = arguments[_key2];
            }

            return _add.apply(undefined, [data].concat(args));
        },
        get: function get(ns, fn) {
            return _get(data, ns, fn);
        },
        getById: function getById(id) {
            return _getById(data, id);
        },
        removeById: function removeById(id) {
            return _removeById(data, id);
        },
        remove: function remove(ns, fn) {
            var nsFilter = genNsFilter(ns, fn);
            (0, _removeArrayItem.removeArrayItem)(data, nsFilter, function (item) {
                _removeById(data, item.id);
            });
        },

        forEachValue: function forEachValue(ns, fn) {
            var datas = _get(data, ns, fn);
            datas.forEach(function (data) {
                fn.apply(undefined, (0, _toConsumableArray3.default)(data.value).concat([data]));
            });
        }
    };
};

exports.default = genNs;